package com.edie.customview.modular;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.edie.customview.BuildConfig;
import com.edie.customview.R;
import com.edie.customview.data.entity.UserEntity;
import com.edie.customview.data.net.BaseObserver;
import com.edie.customview.data.net.NetClient;
import com.edie.customview.data.net.RemoteClient;
import com.edie.customview.data.net.api.NetClient1;
import com.edie.customview.data.net.entity.UserInfoEntity;
import com.edie.customview.util.MD5;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NetRequestActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "NetRequestActivity";
    private TextView resultTv;
    private String mHashid;
    private String mUid;

    public static void start(Context context) {
        context.startActivity(new Intent(context, NetRequestActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_request);
        initView();
    }

    private void initView() {
        resultTv = (TextView) findViewById(R.id.result_tv);
        findViewById(R.id.normal_btn).setOnClickListener(this);
        findViewById(R.id.error_btn).setOnClickListener(this);
        findViewById(R.id.info_btn).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.normal_btn:
                normalRequest();
                break;
            case R.id.error_btn:
                errorRequest();
                break;
            case R.id.info_btn:
                infoRequest();
                break;
        }
    }

    private void normalRequest() {
        String miei = "";
        String model = Build.MODEL;
        String username = "18135705969";
        String password = "123456";
        //以下是必须参数
        String device = "android";
        String deviceid = "";
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String version = "";
        //按ascii排序
        String hash = getHash(device, deviceid, miei, model, password, time, username, version);
        NetClient1.getInstance()
                .createUserApi()
                .postLogin(device, deviceid, time, hash, version, miei, model, username, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe: ");
                    }

                    @Override
                    public void onNext(UserEntity userEntity) {
                        Log.d(TAG, "onNext: ");
                        resultTv.setText(userEntity.toString());
                        mHashid = userEntity.data.hashid;
                        mUid = userEntity.data.uid;
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: ", e);
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete: ");
                    }
                });
    }

    private void errorRequest() {
        String miei = "";
        String model = Build.MODEL;
        String username = "18135705969";
        String password = "123456";
        //以下是必须参数
        String device = "android";
        String deviceid = "";
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String version = "";
        //按ascii排序
        String hash = getHash(device, deviceid, miei, model, password, time, username, version);
        NetClient.getInstance()
                .createUserApi()
                .postErrorLogin(device, deviceid, time, hash, version, miei, model, username, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe: ");
                    }

                    @Override
                    public void onNext(UserEntity userEntity) {
                        Log.d(TAG, "onNext: ");
                        resultTv.setText(userEntity.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: ", e);
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete: ");
                    }
                });
    }

    private void infoRequest() {
        //以下是必须参数
//        String device = "android";
//        String deviceid = "";
//        String time = String.valueOf(System.currentTimeMillis() / 1000);
//        String version = BuildConfig.VERSION_NAME;
//        //按ascii排序
//        String hash = getHash(device, deviceid, hashid, time, uid, version);

//        NetClient.getInstance()
//                .createUserApi()
//                .loadUserInfo(mHashid, mUid)
//                .subscribeOn(Schedulers.newThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });

        RemoteClient.getInstance()
                .postLogin(mHashid, mUid)
                .subscribe(new BaseObserver<UserInfoEntity>() {
                    @Override
                    public void onNext(UserInfoEntity userInfoEntity) {
                        Log.d(TAG, "onNext: ");
                        resultTv.setText(userInfoEntity.toString());
                    }
                });
    }

    public static String getHash(String... values) {
        String securityCode = "8cUoeQ31Q3C+5DQEDkYD55RA++GP0EwJgE95gaxbZlo=";
        StringBuilder sb = new StringBuilder();
        for (String s : values) {
            sb.append(s);
        }
        sb.append(securityCode);
        return MD5.getStringMD5(sb.toString());
    }
}
